package edu.sdcet.lx01.controller;


import edu.sdcet.lx01.entity.Student;
import edu.sdcet.lx01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    // 获取所有学生信息
    @GetMapping(value = "/getAllStudents", produces = "application/json")
    public List<Student> getAllStudents(){
        List<Student> students = studentService.getAllStudents();
        return students;
    }

    // 根据学生ID获取学生信息
    @GetMapping(value = "/getStudentById/{student_id}", produces = "application/json")
    public Student getStudentById(@PathVariable("student_id") String StudentId){
        System.out.println("student_id="+StudentId);
        Student student = studentService.getStudentById(StudentId);
        System.out.println("student="+student);
        return student;
    }
    
    // 根据学生ID更新学生信息
    @PutMapping(value = "/updateStudentById/{student_id}", produces = "application/json")
    public boolean updateStudentById(@PathVariable("student_id") String studentId, @RequestBody Student student){
        System.out.println("更新学生信息：studentId="+studentId+"，学生信息="+student);
        boolean result = studentService.updateStudentById(studentId, student);
        System.out.println("更新结果："+result);
        return result;
    }

    // 根据学生ID删除学生信息
    @DeleteMapping(value = "/deleteStudentById/{student_id}", produces = "application/json")
    public boolean deleteStudentById(@PathVariable("student_id") String studentId){
        System.out.println("删除学生信息：studentId="+studentId);
        boolean result = studentService.deleteStudentById(studentId);
        System.out.println("删除结果："+result);
        return result;
    }

    //新增学生信息
    @PostMapping(value = "/addStudent", produces = "application/json")
    public boolean addStudent(@RequestBody Student student){
        System.out.println("新增学生信息："+student);
        boolean result = studentService.addStudent(student);
        System.out.println("新增结果："+result);
        return result;
    }
}
